package com.adcc.msgsimulator.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author a123
 */
public class ConfigMapUtil {

    public static final Properties p = new Properties();
    public static final String path = "config.properties";

     static  {
        //转换成流
        InputStream inputStream = ConfigMapUtil.class.getClassLoader().getResourceAsStream(path);
         try {
             p.load(inputStream);
         } catch (IOException e) {
             e.printStackTrace();
         }
     }

    /**
     * 通过key获取value
     * @param key
     * @return
     */
    public static String get(String key) {
        return p.getProperty(key);
    }

    /**
     * 修改或者新增key
     * @param key
     * @param value
     */
    public static void update(String key, String value) {
        p.setProperty(key, value);
        FileOutputStream oFile = null;
        try {
            oFile = new FileOutputStream(ConfigMapUtil.class.getClassLoader().getResource(path).getPath());
            //将Properties中的属性列表（键和元素对）写入输出流
            p.store(oFile, "update");
            String filePath=new File("").getCanonicalPath();
            oFile=new FileOutputStream(filePath+"/src/main/resources/config.properties");
            p.store(oFile, "update");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                oFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
